﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using enqueteJwVd;
using enqueteJwVd.DAL;
using enqueteJwVd.Services;
using enqueteJwVd.ViewModels;

namespace enqueteJwVd.Controllers
{ 
    public class SurveyQuestionGroupController : Controller
    {
        UnitOfWork unitOfWork = new UnitOfWork();
        ISurveyQuestionGroupService surveyQuestionGroupService;

          public SurveyQuestionGroupController(ISurveyQuestionGroupService surveyQuestionGroupService)
        {
            this.surveyQuestionGroupService = surveyQuestionGroupService;
        }

        public SurveyQuestionGroupController()
        {
            
        }
        //
        // GET: /SurveyQuestionGroup/

        public ViewResult Index()
        {
            var surveyQuestionGroups = surveyQuestionGroupService.GetAllSurveyQuestionGroups();
            var viewModel = new SurveyQuestionGroupIndexViewModel() { SurveyQuestionGroups = surveyQuestionGroups };
            return View(viewModel);
        }

        //
        // GET: /SurveyQuestionGroup/Details/5

        public ViewResult Details(int id)
        {
            SurveyQuestionGroup surveyquestiongroup = unitOfWork.SurveyQuestionGroupRepository.GetById(id);
            return View(surveyquestiongroup);
        }

        //
        // GET: /SurveyQuestionGroup/Create

        public ActionResult Create()
        {
            ViewBag.QuestionGroupFkId = new SelectList(unitOfWork.QuestionGroupRepository.Get(), "Id", "GroupSequence ");
            ViewBag.SurveyFKId = new SelectList(unitOfWork.SurveyRepository.Get(), "Id", "Title"); 
            return View();
        } 

        //
        // POST: /SurveyQuestionGroup/Create

        [HttpPost]
        public ActionResult Create([Bind(Exclude = "Id")] SurveyQuestionGroup surveyquestiongroup)
        {
            if (ModelState.IsValid)
            {
                if (this.CheckList(surveyquestiongroup).Count() == 0)
                {
                    unitOfWork.SurveyQuestionGroupRepository.Add(surveyquestiongroup);
                    unitOfWork.SaveChanges();
                    return RedirectToAction("Index");
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "This survey - QuestionGroup combination already exists");
                }
            }

            ViewBag.QuestionGroupFkId = new SelectList(unitOfWork.QuestionGroupRepository.Get(), "Id", "GroupSequence", surveyquestiongroup.QuestionGroupFkId);
            ViewBag.SurveyFKId = new SelectList(unitOfWork.SurveyRepository.Get(), "Id", "Title", surveyquestiongroup.SurveyFKId);
            return View(surveyquestiongroup);
        }
        
        //
        // GET: /SurveyQuestionGroup/Edit/5
 
        public ActionResult Edit(int id)
        {
            SurveyQuestionGroup surveyquestiongroup = unitOfWork.SurveyQuestionGroupRepository.GetById(id);
            ViewBag.QuestionGroupFkId = new SelectList(unitOfWork.QuestionGroupRepository.Get(), "Id", "GroupSequence ");
            ViewBag.SurveyFKId = new SelectList(unitOfWork.SurveyRepository.Get(), "Id", "Title");
            return View(surveyquestiongroup);
        }

        private List<SurveyQuestionGroup> CheckList(SurveyQuestionGroup surveyQuestionGroup)
        {
            List<SurveyQuestionGroup> checkList = new List<SurveyQuestionGroup>();
            IQueryable<SurveyQuestionGroup> list = unitOfWork.SurveyQuestionGroupRepository.GetAll();
            foreach (SurveyQuestionGroup uqg in list)
            {
                if (uqg.QuestionGroupFkId == surveyQuestionGroup.QuestionGroupFkId && uqg.SurveyFKId == surveyQuestionGroup.SurveyFKId)
                {
                    checkList.Add(uqg);// zou normaal max. 1 member mogen bevatten

                }
            }
            return checkList;
        }

        //
        // POST: /SurveyQuestionGroup/Edit/5

        [HttpPost]
        public ActionResult Edit(SurveyQuestionGroup surveyquestiongroup)
        {
            if (ModelState.IsValid)
            {
                if (this.CheckList(surveyquestiongroup).Count() == 0)
                {//checken of combinatie al bestaat!!
                    unitOfWork.SurveyQuestionGroupRepository.Update(surveyquestiongroup);
                    unitOfWork.SaveChanges();
                    return RedirectToAction("Index");
                }
                else
                {
                    //http://stackoverflow.com/questions/5739362/modelstate-addmodelerror-how-can-i-add-an-error-that-isnt-for-a-property
                    ModelState.AddModelError(string.Empty, "This survey - questionGroup combination already exists");
                }

            }
            ViewBag.QuestionGroupFkId = new SelectList(unitOfWork.QuestionGroupRepository.Get(), "Id", "GroupSequence", surveyquestiongroup.QuestionGroupFkId);
            ViewBag.SurveyFKId = new SelectList(unitOfWork.SurveyRepository.Get(), "Id", "Title", surveyquestiongroup.SurveyFKId);
            return View(surveyquestiongroup);
            
        }

        //
        // GET: /SurveyQuestionGroup/Delete/5
 
        public ActionResult Delete(int id)
        {
            SurveyQuestionGroup surveyquestiongroup = unitOfWork.SurveyQuestionGroupRepository.GetById(id);
            return View(surveyquestiongroup);
        }

        //
        // POST: /SurveyQuestionGroup/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            SurveyQuestionGroup surveyquestiongroup = unitOfWork.SurveyQuestionGroupRepository.GetById(id);
            unitOfWork.SurveyQuestionGroupRepository.Delete(surveyquestiongroup);
            unitOfWork.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            unitOfWork.Dispose();
            base.Dispose(disposing);
        }
    }
}